from models import *
from common.response import Response


#Response
def response2dict(self):
    return {
        'code': self.code,
        'msg': self.msg,
        'data': self.data
    }


# User
def user2dict(self):
    return {
        'id': self.id,
        'username': self.username,
        'password': self.password,
        'power': self.power,
        'picture': self.picture
    }


def dict2user(d):
    return User(d['username'], d['password'], d['power'], d['picture'])


# Book
def book2dict(self):
    return {
        'id': self.id,
        'name': self.name,
        'author': self.author,
        'introduction': self.introduction,
        'picture': self.picture,
        'num': self.num,
        'price': self.price,
        'score': self.score,
        'sort': self.sort,
        'code': self.code,
        'location': self.location
    }


def dict2book(d):
    return Book(d['name'], d['author'], d['introduction'], d['picture'], d['num'], d['price'], d['score'], d['sort'],
                d['code'], d['location'])


# Borrow
def borrow2dict(self):
    return {
        'id':self.id,
        'user_id': self.user_id,
        'user_name': self.user_name,
        'book_id': self.book_id,
        'borrow_time': self.borrow_time,
        'return_time': self.return_time,
        'num': self.num
    }


def dict2borrow(d):
    return Borrow(d['user_id'], d['book_id'], d['num'])


# Buy
def buy2dict(self):
    return {
        'buy_id': self.buy_id,
        'user_from_id':self.user_from_id,
        'user_to_id': self.user_to_id,
        'library_name': self.library_name,
        'borrow_time': self.borrow_time,
        'return_time2': self.return_time,
        'num': self.num,
        'location': self.location
    }


def dict2buy(d):
    return Buy( d['buy_id'],d['user_from_id'], d['user_to_id'] ,d['library_name'], d['num'], d['location'])


# Comment
def comment2dict(self):
    return {
        'id': self.id,
        'book_id': self.book_id,
        'name': self.name,
        'content': self.content,
        'score': self.score,
        'picture': self.picture
    }


def dict2comment(d):
    return Comment(d['book_id'], d['name'], d['content'], d['score'], d['picture'])


# Information
def information2dict(self):
    return {
        'id': self.id,
        'user_id': self.user_id,
        'sort': self.sort,
        'time': self.time,
        'content': self.content
    }


def dict2information(d):
    return Information(d['user_id'], d['sort'], d['content'])


# Library
def library2dict(self):
    return {
        'user_id': self.user_id,
        'user_name': self.user_name,
        'library_name': self.library_name,
        'author': self.author,
        'information': self.information,
        'picture': self.picture,
        'num': self.num,
        'price': self.price
    }


def dict2library(d):
    return Library(d['library_name'], d['author'], d['information'], d['picture'], d['num'], d['price'], d['user_id'],
                   d['user_name'])


def borrowVO2dict(self):
    return {
        'id': self.id,
        'user_id': self.user_id,
        'book_name': self.book_name,
        'book_id': self.book_id,
        'borrow_time': self.borrow_time,
        'return_time': self.return_time,
        'num': self.num,
        'remain_num': self.remain_num
    }

def returnVO2dict(self):
    return {
        'id': self.id,
        'user_id': self.user_id,
        'book_name': self.book_name,
        'book_id': self.book_id,
        'borrow_time': self.borrow_time,
        'return_time': self.return_time,
        'num': self.num,
        'remain_num': self.remain_num
    }

def buyVO2dict(self):
    return {
        'id': self.buy_id,
        'user_from_id': self.user_from_id,
        'user_to_id': self.user_to_id,
        'book_name': self.library_name,
        'borrow_time': self.borrow_time,
        'return_time': self.return_time,
        'num': self.num,
        'library_from_remain_num': self.library_from_num,
        'library_from_price': self.library_from_price
    }